﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

using ECS.Framework;
using ECS.Framework.ReportCR;
using ECS.BLL.Entity;
using ECS.DAL.Component;

namespace E_ICS.E_ICS
{
    public partial class frmPreManifest : Form
    {
        #region private variables
        /// <summary>
        /// crystal report components
        /// </summary>
        private ReportModule _reportModule = null;
        /// <summary>
        /// BLL pre-manifest class
        /// </summary>
        private PreManifestComponent _manifest = null;
        private ReportCriteria param = null;
        #endregion

        #region Form event
        
        public frmPreManifest()
        {
            InitializeComponent();
            
        }
        private void frmPreManifest_Load(object sender, EventArgs e)
        {
            _manifest = new PreManifestComponent();
            param = new ReportCriteria();
            InitReport();
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //InitReport();
            if (!ValidateFilter())
            {
                return;
            }
            ShowReport();
        }
        private void btnClearAll_Click(object sender, EventArgs e)
        {
            txtPrefix.Text = string.Empty;
            txtSerial.Text = string.Empty;
            txtHAWB.Text = string.Empty;
            txtCarrier.Text = string.Empty;
            txtFlightNo.Text = string.Empty;
            
        }
        #endregion

        #region Private private methods

        /// <summary>
        /// Initialize report controls
        /// </summary>
        private void InitReport()
        {
            try
            {
                if (_reportModule == null)
                {
                    _reportModule = new ReportModule();
                    _reportModule.InitialReport("Report/PreManifestReport.rpt");
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        /// <summary>
        /// validate report filter
        /// </summary>
        /// <returns></returns>
        private bool ValidateFilter()
        {
            //if (string.IsNullOrEmpty(txtCarrier.Text))
            //{
            //    CommonMessageBox.ShowMessageWarning("Invalid or Empty Carrier");
            //    txtCarrier.Focus();
            //    return false;
            //}
            //if (string.IsNullOrEmpty(txtFlightNo.Text))
            //{
            //    CommonMessageBox.ShowMessageWarning("Invalid or Empty Flight Number");
            //    txtFlightNo.Focus();
            //    return false;
            //}
            return true;
        }
        /// <summary>
        /// search then show report
        /// </summary>
        private void ShowReport()
        {
            try
            {
                
                param.Carrier = txtCarrier.Text;
                param.FlightNo = txtFlightNo.Text;
                param.FlightDate = dtpFlightDate.Value;
                param.MawbPrefix = txtPrefix.Text;
                param.MawbSN = txtSerial.Text;
                param.Hawb = txtHAWB.Text;
                _reportModule.InitialReport("Report/PreManifestReport.rpt");
                _reportModule.LoadReportData(_manifest.GetPreManifestData(param));
                _reportModule.SetReportParameter("FlightNumber", txtFlightNo.Text);
                _reportModule.ShowReportViewer(ref viewer);
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        
        
        #endregion

        

        /*
            ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            cryRpt.Load("Report/PreManifestReport.rpt");
            //
            //   cryRpt.Load("D:\\Google Drive\\Google Drive\\Code\\Test\\Test_crystal report\\Test_crystal report\\Test_crystal report\\CrystalReport2.rpt");

            crConnectionInfo.ServerName = "192.168.1.210";
            crConnectionInfo.DatabaseName = "TestDB";
            crConnectionInfo.UserID = "dev01";
            crConnectionInfo.Password = "password";


            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }
            */


        /*


        //------------------------------------------------------------------
        // กำหนด พารามิเตอร์ตัวแรก UserID

        ParameterFieldDefinitions crParameterFieldDefinitions;
        ParameterFieldDefinition crParameterFieldDefinition;
        ParameterValues crParameterValues = new ParameterValues();

        ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

        crParameterDiscreteValue.Value = "user1";
        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
        crParameterFieldDefinition = crParameterFieldDefinitions["@UserID"];
        crParameterValues = crParameterFieldDefinition.CurrentValues;

        crParameterValues.Clear();
        crParameterValues.Add(crParameterDiscreteValue);
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

        //   -------------------------------------
        // กำหนด พารามิเตอร์ตัวที่ 2 Password

        crParameterDiscreteValue.Value = "user1";
        crParameterFieldDefinition = crParameterFieldDefinitions["@Password"];
        crParameterValues = crParameterFieldDefinition.CurrentValues;


        crParameterValues.Clear();
        crParameterValues.Add(crParameterDiscreteValue);
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

        //---------------------------------------------------------------
        //bacup ไว้แบบ Full


        //ParameterFieldDefinitions crParameterFieldDefinition2s2;
        //ParameterFieldDefinition crParameterFieldDefinition2;
        //ParameterValues crParameterValues2 = new ParameterValues();

        //ParameterDiscreteValue crParameterDiscreteValue2 = new ParameterDiscreteValue();

        //crParameterDiscreteValue2.Value = "user1";
        //crParameterFieldDefinition2s2 = cryRpt.DataDefinition.ParameterFields;
        //crParameterFieldDefinition2 = crParameterFieldDefinition2s2["@UserID"];
        //crParameterValues2 = crParameterFieldDefinition2.CurrentValues;

        //crParameterValues2.Clear();
        //crParameterValues2.Add(crParameterDiscreteValue2);
        //crParameterFieldDefinition2.ApplyCurrentValues(crParameterValues2);

        //---------------------------------------------------------------
*/

        //crystalReportViewer1.ReportSource = cryRpt;
        //crystalReportViewer1.Refresh();


        //cryRpt.PrintToPrinter(1, true, 1, 1);
    }
}
